﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VehicleManagement.Models;
using VehicleManagement.ControllersServices;
using System.Collections;

namespace VehicleManagement.Controllers
{
    //[CustomerAuthorize(Roles = "管理员,负责人,申请人,财务人员,司机")]
    public class MessageController : Controller
    {
        MessageService messageService = new MessageService();
        Hashtable ht = new Hashtable();


        //
        // GET: /Message/

        public ViewResult Index()
        {
            if (Session["UserID"] == null)
                Response.Redirect("/Home/Login");

            ht.Add("receiverID", Session["UserID"]);
            ht.Add("receiverRole", HomeController.userRole);

            IList<Message> lstModel = messageService.GetMyMessage(ht);
            return View(lstModel);
        }



        //
        // GET: /Message/Details/5

        public ViewResult Details(int id)
        {

            IList<Message> lstModel = messageService.GetMessageByID(id);
            Message message = lstModel.Where(u => u.MessageID == id).First();
            return View(message);
        }

        //
        // GET: /Message/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Message/Create

        [HttpPost]
        public ActionResult Create(Message message)
        {
            if (ModelState.IsValid)
            {
                object obj = messageService.InsertMessage(message);
                return RedirectToAction("Index");
            }

            return View(message);
        }


        //
        // GET: /Message/Edit/5

        public ActionResult Edit(int id)
        {

            IList<Message> lstModel = messageService.GetMessageByID(id);
            Message message = lstModel.Where(u => u.MessageID == id).First();
            return View(message);
        }

        //
        // POST: /Message/Edit/5

        [HttpPost]
        public ActionResult Edit(Message message)
        {
            if (ModelState.IsValid)
            {
                messageService.UpdateMessage(message);
                return RedirectToAction("Index");
            }
            return View(message);
        }

        //
        // POST: /Message/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            int i = messageService.DeleteMessage(id);
            if (i > 0)
            {
                return Json(new { Result = "true", Message = "删除成功" });
            }
            else
            {
                return Json(new { Result = "false", Message = "删除失败" });
            }
        }

    }
}
